import { ref } from 'vue'
import type { FileInfo } from '../types'
import { ProcessStatus } from '../types'
import { watermarkApi } from '../api/watermark'

export function useUpload() {
  const fileList = ref<FileInfo[]>([])
  const isUploading = ref(false)

  // 添加文件
  const addFile = (file: File) => {
    const fileInfo: FileInfo = {
      name: file.name,
      size: file.size,
      type: file.type,
      status: ProcessStatus.IDLE,
      url: URL.createObjectURL(file)
    }
    
    fileList.value.push(fileInfo)
    return fileInfo
  }

  // 上传文件
  const uploadFile = async (file: FileInfo, uploadedFile: File) => {
    if (!uploadedFile) return
    
    try {
      file.status = ProcessStatus.UPLOADING
      isUploading.value = true
      
      const response = await watermarkApi.removeWatermark(uploadedFile, (progress) => {
        file.progress = progress
      })
      
      file.status = ProcessStatus.SUCCESS
      
      // 设置结果URL
      if (response && response.resultUrl) {
        file.resultUrl = response.resultUrl
      }
      
      return response
    } catch (error: any) {
      file.status = ProcessStatus.ERROR
      file.error = error.message || '上传失败'
      throw error
    } finally {
      isUploading.value = false
    }
  }

  // 清除文件
  const clearFiles = () => {
    fileList.value = []
  }

  return {
    fileList,
    isUploading,
    addFile,
    uploadFile,
    clearFiles
  }
} 